/***************************************************************
 *
 * (c) 2008 Markus Dittrich 
 *
 * This program is free software; you can redistribute it 
 * and/or modify it under the terms of the GNU General Public 
 * License Version 3 as published by the Free Software Foundation. 
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License Version 3 for more details.
 *
 * You should have received a copy of the GNU General Public 
 * License along with this program; if not, write to the Free 
 * Software Foundation, Inc., 59 Temple Place - Suite 330, 
 * Boston, MA 02111-1307, USA.
 *
 ****************************************************************/

/** STL includes */
#include <iostream>

/** local includes */
#include "basic_definitions.h"
#include "data_object.h"
#include "utilities.h"

/** pull in a few symbols from std namespace */
using std::vector;


//---------------------------------------------------------------
// constructor
//---------------------------------------------------------------
DataObject::DataObject(unsigned int numColumns)
  :
  Data_(vector<vector<double> >(
	numColumns, vector<double>())),
  EmptyVector_(vector<double>(0,0.0)),
  needInitialSetup_(true),
  NumColumns_(numColumns),
  XDataColumn_(ELEMENTS_UNSET),
  YDataColumn_(ELEMENTS_UNSET)
{
  /* for efficiency reason reserve an initially large
   * chunk of space */
 FOREACH(vector<double>& element, Data_)
 {
   element.reserve(RESERVED_SIZE);
 }
}
 
//----------------------------------------------------------------
// destructor
//----------------------------------------------------------------
DataObject::~DataObject()
{
#ifdef DEBUG
  std::cout << "Deleting DataObject" << std::endl;
#endif
}

/*****************************************************************
 *
 * public member functions
 *
 ****************************************************************/

//----------------------------------------------------------------
// append member function:
// takes a vector<double> and adds NumItems_ to the respective
// columns of Data_ padding missing elements with zero if 
// neccessary.
//----------------------------------------------------------------
void DataObject::append(const std::vector<double>& lineItems)
{
  assert( lineItems.size() <= NumColumns_ );

  int column = 0;
  FOREACH(double item, lineItems)
  {
    Data_[column++].push_back(item);
  }

  /* pad with zeros */
  while ( column < NumColumns_ )
  {
    Data_[column++].push_back(0.0);
  }
}



//----------------------------------------------------------------
// return data to be plotted on x axis
//----------------------------------------------------------------
const vector<double>& DataObject::get_x_data() const
{
  if ( needInitialSetup_ )
  {
    setup_();
    needInitialSetup_ = false;
  }
  assert ( XDataColumn_ != ELEMENTS_UNSET );

  if ( XDataColumn_ == ELEMENT_COUNT )
  {
    return EmptyVector_;
  }
  else
  {
    return Data_[XDataColumn_];
  }
}


//----------------------------------------------------------------
// return data to be plotted on y axis
//----------------------------------------------------------------
const vector<double>& DataObject::get_y_data() const
{
  if ( needInitialSetup_ )
  {
    setup_();
    needInitialSetup_ = false;
  }
  assert ( YDataColumn_ != ELEMENTS_UNSET );
 
  if ( YDataColumn_ == ELEMENT_COUNT )
  {
    return EmptyVector_;
  }
  else
  {
    return Data_[YDataColumn_];
  }
}


//----------------------------------------------------------------
// returns all data contained in a particular row 
//----------------------------------------------------------------
const vector<double> DataObject::get_row_data(int row) const
{
  assert( row < get_num_rows() );

  vector<double> rowData;
  rowData.reserve(NumColumns_);

  FOREACH(const vector<double> column, Data_)
  {
    rowData.push_back(column[row]);
  }

  return rowData;
}


//---------------------------------------------------------------
// returns the currently active x and y columns 
//---------------------------------------------------------------

/* x */
int DataObject::get_active_x_column() const
{
  if ( needInitialSetup_ )
  {
    setup_();
    needInitialSetup_ = false;
  }
  assert ( XDataColumn_ != ELEMENTS_UNSET );

  return XDataColumn_;
}


/* y */
int DataObject::get_active_y_column() const
{
  if ( needInitialSetup_ )
  {
    setup_();
    needInitialSetup_ = false;
  }
  assert ( YDataColumn_ != ELEMENTS_UNSET );

  return YDataColumn_;
}


/*****************************************************************
 *
 * private member functions
 *
 ****************************************************************/

//----------------------------------------------------------------
// wrapper function calling all setup routines; this is needed
// after Object initialization or once the X/YDataColumn fields
// have been switched
//----------------------------------------------------------------
void DataObject::setup_() const
{
  /* use swap trick to trim excess capacity */
  FOREACH(vector<double>& element, Data_)
  {
    vector<double>(element).swap(element);
  }

  assign_columns_();
}

//----------------------------------------------------------------
// fill X/YData_ arrays; for a one column file, both the x and y
// data arrays point to the same (zeroth) column; for two or more
// columns x gets the zeroth and y the first column
//----------------------------------------------------------------
void DataObject::assign_columns_() const
{
  if ( NumColumns_ == 1 )
  {
    XDataColumn_ = ELEMENT_COUNT;
    YDataColumn_ = 0;
  }
  else if ( NumColumns_ > 1 )
  {
    XDataColumn_ = 0;
    YDataColumn_ = 1;
  }
}
